package gov.dhs.cis.soa.generators;

import gov.dhs.cis.soa.Constants;
import gov.dhs.cis.soa.SOAData;
import gov.dhs.cis.soa.Utils;
import gov.dhs.cis.soa.Constants.EntityType;
import gov.dhs.cis.soa.data.AbstractSOAData;
import gov.dhs.cis.soa.data.Actor;
import gov.dhs.cis.soa.data.Agent;
import gov.dhs.cis.soa.data.Application;
import gov.dhs.cis.soa.data.BusinessProcess;
import gov.dhs.cis.soa.data.DataClass;
import gov.dhs.cis.soa.data.Description;
import gov.dhs.cis.soa.data.EntitiesReferences;
import gov.dhs.cis.soa.data.InfrastructureLS;
import gov.dhs.cis.soa.data.InfrastructureLSInst;
import gov.dhs.cis.soa.data.InfrastructurePS;
import gov.dhs.cis.soa.data.IntegrationComp;
import gov.dhs.cis.soa.data.IntegrationSW;
import gov.dhs.cis.soa.data.Interface;
import gov.dhs.cis.soa.data.Processes;
import gov.dhs.cis.soa.data.RelatedDataReference;
import gov.dhs.cis.soa.data.RelatedProperties;
import gov.dhs.cis.soa.data.SecurityComp;
import gov.dhs.cis.soa.data.Service;
import gov.dhs.cis.soa.data.ServiceOperation;
import gov.dhs.cis.soa.data.UseCase;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;


public class UMLFileCreator {
	static final Logger loger = Logger.getLogger(UMLFileCreator.class);

	private static final String UML_MODELS_NAME = "USCIS IOE UML Model V1";
	private static final String UML_MODELS_FILE_NAME = "USCISIOEModel1.uml";			
	
	private static final String REL_FOLDER_NAME = "00_Releases";
	private static final String ACTORS_FOLDER_NAME = "001_Actors";
	private static final String UI_INTERFACES_FOLDER_NAME = "002_User Interfaces";
	private static final String WIPT_FOLDER_NAME = "003_Management Functions";
	private static final String SYS_INTERFACES_FOLDER_NAME = "004_System Interfaces";
	private static final String II_INTERFACES_FOLDER_NAME = "005_Internal Interfaces";
	private static final String PROCESSES_FOLDER_NAME = "006_Processes";
	private static final String SERVES_FOLDER_NAME = "007_Services";
	private static final String AGENTS_FOLDER_NAME = "008_Agents";
	private static final String APPS_FOLDER_NAME = "009_Applications";
	private static final String DATA_FOLDER_NAME = "010_DataClasses";
	private static final String HOST_INTG_FOLDER_NAME = "011_Hosting Config";
	private static final String SEC_INTG_FOLDER_NAME = "012_Security Config";
	private static final String INT_INTG_FOLDER_NAME = "013_Integration Config";
	private static final String SW_INTG_FOLDER_NAME = "014_Software Packages";
	private static final String INF_FOLDER_NAME = "015_Infrastructure Layer";
	private static final String USECASE_FOLDER_NAME = "016_UseCases";
	
	private static final String BUSPROC_FOLDER_NAME = "017_Business Process";

	private static final String COMPID_FOLDER_NAME = "020_Estimating Components";

	private static final String SECCP_FOLDER_NAME = "099_Security Layer";
	//private static final String TECH_FOLDER_NAME = "000_Technologies";
	SOAData soaData = null;
	
	List<String> relInModel = new ArrayList<String>();
	private String outputDir = null;
	
	//private int localID = 1;
	
	public UMLFileCreator(SOAData sData, String outputDir) {
		soaData = sData;
		this.outputDir = outputDir;
	}
	
	public UMLFileCreator(String outputDir) {
		//read services..
		this.outputDir = outputDir;
	}

	private void transform(Document document, String fileName) throws Exception {
	    TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        DOMSource source = new DOMSource(document);
        OutputStream fos = null;
        if(!Utils.isEmpty(fileName)) {
        	fos = new  FileOutputStream(fileName);
        } else {
        	fos = System.out;       	
        }
    	StreamResult result =  new StreamResult(fos);
        transformer.transform(source, result);
	}

	private Element appendRootPkgElement(Document document, Element rootElement, String pkgId, String name) {
		Element pkgElement = document.createElement("packagedElement");			
		pkgElement.setAttribute("xmi:type", "uml:Package");
		pkgElement.setAttribute("xmi:id", pkgId);
		pkgElement.setAttribute("name", name);
		rootElement.appendChild(pkgElement);		
		return pkgElement;
	}

	private Element appendPkgElement(Document document, Element rootNode, String umlClassName, String classId, String className, String steroType, Description description){
		Element childPkgElement = document.createElement("packagedElement");
		childPkgElement.setAttribute("xmi:type", umlClassName);
		childPkgElement.setAttribute("xmi:id", classId);
		childPkgElement.setAttribute("name", className);

		if(steroType != null)
			appendAnnotateNode(document, childPkgElement, classId, steroType);

		rootNode.appendChild(childPkgElement);
		//now add the description if any..
		if(description != null) {
			appendDescription(document, childPkgElement, description);
		}
		return childPkgElement;
	}

	private Element appendDescription(Document document, Element rootElement, Description description) {
		Element commentElement = (Element) rootElement.appendChild(document.createElement("ownedComment"));
		//commentElement.setAttribute("xmi:id", "Comment_"+ description.id);
		commentElement.setAttribute("xmi:id", description.getUMLId(rootElement.getAttribute("name")));
		commentElement.setAttribute("annotatedElement", rootElement.getAttribute("xmi:id"));
		commentElement = (Element) commentElement.appendChild(document.createElement("body"));
		commentElement.appendChild(document.createTextNode(description.descTxt));
		return commentElement;
	}

	private Element appendAbstractActorNode(Document document, Element rootNode, String classId, String className, String steroType, Description description){
		Element childPkgElement = appendActorNode(document, rootNode, classId, className, steroType, description);
		childPkgElement.setAttribute("isAbstract", "true");
		return childPkgElement;
	}

	private Element appendUseCaseNode(Document document, Element rootNode, String classId, String className, String steroType, Description description){
		Element childElement = appendPkgElement(document, rootNode, "uml:UseCase", classId, className, steroType, description);
		return childElement;
	}
	
	private Element appendActorNode(Document document, Element rootNode, String classId, String className, String steroType, Description description){
		Element childElement = appendPkgElement(document, rootNode, "uml:Actor", classId, className, steroType, description);
		return childElement;
	}

	private Element appendClassNode(Document document, Element rootNode, String classId, String className, String steroType, Description description){
		Element childElement = appendPkgElement(document, rootNode, "uml:Class", classId, className, steroType, description);
		return childElement;
	}

	private Element appendComponentNode(Document document, Element rootNode, String classId, String className, String steroType, Description description){
		Element childElement = appendPkgElement(document, rootNode, "uml:Component", classId, className, steroType, description);
		return childElement;
	}
	
	private Element appendAbstractClassNode(Document document, Element rootNode, String classId, String className, String steroType, Description description){
		Element childPkgElement = appendClassNode(document, rootNode, classId, className, steroType, description);
		childPkgElement.setAttribute("isAbstract", "true");
		return childPkgElement;
	}

	private Element appendActivityNode(Document document, Element rootNode, String classId, String className, String steroType, Description description){
		Element childElement = appendPkgElement(document, rootNode, "uml:Activity", classId, className, steroType, description);
		return childElement;
	}
	
	private Element appendNodeElement(Document document, Element rootNode, String umlClassName, String classId, String className, String steroType, Description description){
		Element childPkgElement = document.createElement("node");
		childPkgElement.setAttribute("xmi:type", umlClassName);
		childPkgElement.setAttribute("xmi:id", classId);
		childPkgElement.setAttribute("name", className);

		if(steroType != null)
			appendAnnotateNode(document, childPkgElement, classId, steroType);

		rootNode.appendChild(childPkgElement);
		//now add the description if any..
		if(description != null) {
			appendDescription(document, childPkgElement, description);
		}
		return childPkgElement;
	}

	private Element appendAnnotateNode(Document document, Element rootNode, String parentClassID, String steroType) {
		Element eAnnotElement = document.createElement("eAnnotations");
		eAnnotElement.setAttribute("xmi:id", "Annot_"+ parentClassID);
		eAnnotElement.setAttribute("source", "http://www.eclipse.org/uml2/2.0.0/UML");
		
		Element eAnnotDetailsElement = document.createElement("details");
		eAnnotDetailsElement.setAttribute("xmi:id", "Detail_"+ parentClassID);
		eAnnotDetailsElement.setAttribute("key", steroType);
		
		rootNode.appendChild(eAnnotElement);
		eAnnotElement.appendChild(eAnnotDetailsElement);
		return eAnnotElement;
	}

	private Element appendGeneralizationNode(Document document, Element rootNode, String parentClassId, String genricClassID){
		//Appended child seq in the end to avoid dup ids when more than one child of same type present - 07/13/2010
		int children = 0;
		if(rootNode.getChildNodes() != null)
			children = rootNode.getChildNodes().getLength();
		children++;
		String generalizationID = "Gen_" + parentClassId + "_" + children;
		Element childPkgElement = document.createElement("generalization");
		childPkgElement.setAttribute("xmi:id", generalizationID);
		childPkgElement.setAttribute("general", genricClassID);

		appendAnnotateNode(document, childPkgElement, generalizationID, "of type");
		rootNode.appendChild(childPkgElement);
		return childPkgElement;
	}

	private Element appendDependencyNode(Document document, Element rootNode, String dependencyId, String name, String suppID, String clientID){
		Element childPkgElement = document.createElement("packagedElement");
		childPkgElement.setAttribute("xmi:type", "uml:Dependency");
		//childPkgElement.setAttribute("xmi:id", (dependencyId + "_" + localID++));
		childPkgElement.setAttribute("xmi:id", dependencyId);
		childPkgElement.setAttribute("name", name);
		childPkgElement.setAttribute("supplier", suppID);
		childPkgElement.setAttribute("client", clientID);
		rootNode.appendChild(childPkgElement);
		return childPkgElement;
	}
	
	private Element appendUndirectedAssociationNode(Document document, Element rootNode, String srcUMLId, String destUMLId, String assocName){
		Element childPkgElement = document.createElement("packagedElement");
		childPkgElement.setAttribute("xmi:type", "uml:Association");
		String assocUMLId = "ASSOC_" + srcUMLId + "_" + destUMLId;
		childPkgElement.setAttribute("xmi:id", assocUMLId);
		childPkgElement.setAttribute("name", assocName);
		String ownedID1 = "OWNED_1_" + assocUMLId;
		String ownedID2 = "OWNED_2_" + assocUMLId;
		childPkgElement.setAttribute("memberEnd", (ownedID1 + " " + ownedID2));
		String ownedID = null;
		for(int i = 1; i < 3; i++) {
			Element ownedEndElement = document.createElement("ownedEnd");
			ownedID = (i == 1 ? ownedID1 : ownedID2);
			ownedEndElement.setAttribute("xmi:id", ownedID);
			ownedEndElement.setAttribute("name", "");
			ownedEndElement.setAttribute("visibility", "private");
			ownedEndElement.setAttribute("type", (i == 1 ? srcUMLId : destUMLId));
			ownedEndElement.setAttribute("association", assocUMLId);
			/*
			Element upperElement = document.createElement("upperValue");
			upperElement.setAttribute("xmi:type", "uml:LiteralUnlimitedNatural");
			upperElement.setAttribute("xmi:id", "UP_" + ownedID);
			upperElement.setAttribute("value", "1");
			Element lowerElement = document.createElement("lowerValue");
			lowerElement.setAttribute("xmi:type", "uml:LiteralInteger");
			lowerElement.setAttribute("xmi:id", "LW_" + ownedID);
			lowerElement.setAttribute("value", "1");
			ownedEndElement.appendChild(upperElement);
			ownedEndElement.appendChild(lowerElement);
			*/
			childPkgElement.appendChild(ownedEndElement);
		}
		rootNode.appendChild(childPkgElement);
		return childPkgElement;
	}

	private Element appendCompRealizationNode(Document document, Element rootNode, String name, String parentClassId, String clientClassID){
		String realizationID = "Realize_" + name;
		Element childPkgElement = document.createElement("realization");
		childPkgElement.setAttribute("xmi:id", realizationID);
		childPkgElement.setAttribute("supplier", parentClassId); //Who is the realizer, its ID
		childPkgElement.setAttribute("client", clientClassID);	//to which node this realization appliesTo
		childPkgElement.setAttribute("realizingClassifier", parentClassId);
		//childPkgElement.setAttribute("name", name);
		
		rootNode.appendChild(childPkgElement);
		String exisitValue = rootNode.getAttribute("clientDependency");
		if(Utils.isEmpty(exisitValue))
			exisitValue = realizationID;
		else
			exisitValue += (" " + realizationID);
		rootNode.setAttribute("clientDependency", exisitValue);
		return childPkgElement;
	}

	private Element appendRealizationNode(Document document, Element rootNode, String name, String parentClassId, String clientClassID){
		String realizationID = "Realize_" + name;

		Element childPkgElement = document.createElement("packagedElement");
		childPkgElement.setAttribute("xmi:type", "uml:Realization");
		childPkgElement.setAttribute("xmi:id", realizationID);
		//childPkgElement.setAttribute("name", name);
		childPkgElement.setAttribute("supplier", parentClassId); //Who is the realizer, its ID
		childPkgElement.setAttribute("client", clientClassID);	//to which node this realization appliesTo

		rootNode.appendChild(childPkgElement);
		String exisitValue = rootNode.getAttribute("clientDependency");
		if(Utils.isEmpty(exisitValue))
			exisitValue = realizationID;
		else
			exisitValue += (" " + realizationID);
		rootNode.setAttribute("clientDependency", exisitValue);
		return childPkgElement;
	}
	
	private Element appendPropertyNode(Document document, Element rootNode, RelatedProperties r) {
		if(r.propName.equalsIgnoreCase("COMP_ID"))
			return null;
		
		Element attibElement = document.createElement("ownedAttribute");
		String attribID = r.propName + "_ATTR_"+ r.refEntityType + "_" + r.refEntityId;
		attibElement.setAttribute("xmi:id", attribID);
		attibElement.setAttribute("name", r.propName);
		attibElement.setAttribute("visibility", "public");
		
		Element typeElement = document.createElement("type");
		typeElement.setAttribute("xmi:type", "uml:PrimitiveType");
		typeElement.setAttribute("href", "pathmap://UML_LIBRARIES/UMLPrimitiveTypes.library.uml#String");
		attibElement.appendChild(typeElement);
		
		Element defValElement = document.createElement("defaultValue");
		defValElement.setAttribute("xmi:id", "VAL_"+ attribID);
		defValElement.setAttribute("xmi:type", "uml:LiteralString");
		defValElement.setAttribute("value", r.propValue);
		Element typeElement2 = document.createElement("type");
		typeElement2.setAttribute("xmi:type", "uml:PrimitiveType");
		typeElement2.setAttribute("href", "pathmap://UML_LIBRARIES/UMLPrimitiveTypes.library.uml#String");
		defValElement.appendChild(typeElement2);
		attibElement.appendChild(defValElement);
		
		rootNode.appendChild(attibElement);
		return attibElement;
	}

	private Element appendPropertyNode(Document document, Element rootNode, AbstractSOAData refEntity, String propName, String propValue) {
		if("COMP_ID".equalsIgnoreCase(propName))
			return null;
		Element attibElement = document.createElement("ownedAttribute");
		//String attribID = propName + "_ATTR_"+ refEntityType + "_" + refEntityId;
		String attribID = refEntity.getUMLType() + "_" + refEntity.getHashableId() + "." + propName;
		attribID = "ATTR_" + Utils.hash(attribID);
		attibElement.setAttribute("xmi:id", attribID);
		attibElement.setAttribute("name", propName + "=" + (propValue == null ? "" : propValue));
		attibElement.setAttribute("visibility", "public");
		
		Element typeElement = document.createElement("type");
		typeElement.setAttribute("xmi:type", "uml:PrimitiveType");
		typeElement.setAttribute("href", "pathmap://UML_LIBRARIES/UMLPrimitiveTypes.library.uml#String");
		attibElement.appendChild(typeElement);
		
		rootNode.appendChild(attibElement);
		return attibElement;
	}
/*
	private Element appendOperationNode(Document document, Element rootNode, EntityType refEntityType, String opName, String parentHashableId) {
		String opId = refEntityType + ":" + parentHashableId + "." + opName;	//+ (localID++);
		opId = "OPER_" + Utils.hash(opId);
		return appendOperationNode(document, rootNode, refEntityType, opName, opId);
	}
*/	
	private Element appendOperationNode(Document document, Element rootNode, EntityType refEntityType, String opName, String parentHashableId) {
		Element operationElement = document.createElement("ownedOperation");
		
		//Appended child seq in the end to avoid dup ids when more than one child of same type present - 07/13/2010
		int children = 0;
		if(rootNode.getChildNodes() != null)
			children = rootNode.getChildNodes().getLength();
		children++;
		String opId = refEntityType + ":" + parentHashableId + "." + opName + "_" + children;	//+ (localID++);
		opId = "OPER_" + Utils.hash(opId);
		operationElement.setAttribute("xmi:id", opId);
		
		operationElement.setAttribute("name", opName);
		
		rootNode.appendChild(operationElement);
		return operationElement;
	}

	private Element appendOperationParamNode(Document document, Element rootNode, EntityType refEntityType, String paramName, String parmaTypeUMLId, String direction) {
		Element operationElement = document.createElement("ownedParameter");
		//String opId = "PARAM_" + refEntityType + "_" + (localID++);
		
		//Appended child seq in the end to avoid dup ids when more than one child of same type present - 07/13/2010
		String methodName = rootNode.getAttribute("name");
		if(Utils.isEmpty(methodName))
			methodName = "_";
		String className = ((Element)rootNode.getParentNode()).getAttribute("name");
		if(Utils.isEmpty(className))
			className = "_";
		String opId = refEntityType + ":" + className + "." + methodName + "." + paramName + "." + direction;
		opId = "PARAM_" + Utils.hash(opId);
		operationElement.setAttribute("xmi:id", opId);
		operationElement.setAttribute("name", paramName);
		operationElement.setAttribute("type", parmaTypeUMLId);	//UML Id of the refered class ID
		operationElement.setAttribute("direction", direction); //valid values "in", "out", "inout", "return"
		
		rootNode.appendChild(operationElement);
		return operationElement;
	}

	public void createUMLFile() {
		try {
			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
			Document document = documentBuilder.newDocument();
			Element rootElement = document.createElement("xmi:XMI");
			rootElement.setAttribute("xmlns:xmi", "http://schema.omg.org/spec/XMI/2.1");
			rootElement.setAttribute("xmi:version", "2.1");
			rootElement.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
			rootElement.setAttribute("xmlns:Standard", "http://www.eclipse.org/uml2/schemas/Standard/1");
			rootElement.setAttribute("xmlns:ecore", "http://www.eclipse.org/emf/2002/Ecore");
			rootElement.setAttribute("xmlns:uml", "http://www.eclipse.org/uml2/2.1.0/UML");
			rootElement.setAttribute("xsi:schemaLocation", "http://www.eclipse.org/uml2/schemas/Standard/1");
			document.appendChild(rootElement);

			rootElement = (Element) rootElement.appendChild(document.createElement("uml:Model"));
			rootElement.setAttribute("xmi:id", "Model01");
			rootElement.setAttribute("name", UML_MODELS_NAME);
			rootElement.setAttribute("viewpoint", "");
			Description modelDesc = new Description();
			modelDesc.descTxt = "Generated by Java Code. By Rajesh A Singh on " + new Date();
			appendDescription(document, rootElement, modelDesc);

			Element pkgElement = (Element) rootElement.appendChild(document.createElement("packageImport"));
			pkgElement.setAttribute("xmi:id", "Model01_PkgImp1");
			pkgElement = (Element)pkgElement.appendChild(document.createElement("importedPackage"));
			pkgElement.setAttribute("xmi:type", "uml:Model");
			pkgElement.setAttribute("href", "pathmap://UML_LIBRARIES/UMLPrimitiveTypes.library.uml#_0");
			
			appendAllReleases(document, rootElement);
			appendAllWIPTS(document, rootElement);
//			appendAllTechnologies(document, rootElement);
			if(Constants.bResolveSecConfig)
				appendAllSecurityComponents(document, rootElement);
			appendAllIntegrationComponents(document, rootElement);
			appendAllApplications(document, rootElement);
			appendAllInfrastructureComponents(document, rootElement);
			
			appendAllDataClasses(document, rootElement);;
			appendAllUseCase(document, rootElement);
			appendAllActors(document, rootElement);
			appendAllServices(document, rootElement);
			appendAllInterfaces(document, rootElement);
			appendAllAgents(document, rootElement);
			appendAllProcesses(document, rootElement);
			appendAllEstimatingComponents(document, rootElement);
			
			//No loading of business processes in to the model - RSingh 04/22/2010
			//appendAllBusinessProcs(document, rootElement);
			
			Element profElement = (Element) rootElement.appendChild(document.createElement("profileApplication"));
			profElement.setAttribute("xmi:id", "_kL00VjkGEd-QFYAf3Aa6YQ");
			pkgElement = (Element)profElement.appendChild(document.createElement("eAnnotations"));
			pkgElement.setAttribute("xmi:id", "_kL00VzkGEd-QFYAf3Aa6YQ");
			pkgElement.setAttribute("source", "http://www.eclipse.org/uml2/2.0.0/UML");
			pkgElement = (Element)pkgElement.appendChild(document.createElement("references"));
			pkgElement.setAttribute("xmi:type", "ecore:EPackage");
			pkgElement.setAttribute("href", "pathmap://UML_PROFILES/Standard.profile.uml#_yzU58YinEdqtvbnfB2L_5w");
			pkgElement = (Element)profElement.appendChild(document.createElement("appliedProfile"));
			pkgElement.setAttribute("href", "pathmap://UML_PROFILES/Standard.profile.uml#_0");
			
			transform(document, outputDir + "/" + UML_MODELS_FILE_NAME);			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private String generateDependencyId(EntityType srcEntity, AbstractSOAData srcEntityObj, EntityType refEntity, AbstractSOAData refEntityObj, String relationQual) {
		String retID = "";

//		retID = "DEP_" + srcEntityObj.getUMLId()+ "_";
//		retID += refEntityObj.getUMLId();
		
		retID = (srcEntityObj.getUMLType() + ":" + srcEntityObj.getHashableId());
		retID += (" [" + relationQual + "] ");
		retID += (refEntityObj.getUMLType() + ":" + refEntityObj.getHashableId());
		
		retID = "DEP_" + Utils.hash(retID);

		return retID;
	}

	private String getReleaseId(String relStr) {
		final String releases = "ABCDE";
		if(!Utils.isEmpty(relStr)) {
			if(releases.indexOf(relStr.charAt(0)) > -1) {
				return "REL_" + relStr.charAt(0);
			}
		}
		return null;
	}
	private Element appendAllReleases(Document document, Element rootElement) {
		String[] releases = new String[] { "A;Release A", "B;Release B",
				"C;Release C", "D;Release D", "E;Release E" };
		
		Element pkgElement = appendRootPkgElement(document, rootElement, "ReleasePkg01", REL_FOLDER_NAME);
		for(String rel : releases) {
			String[] tokens = rel.split("[;]");
			String relId = getReleaseId(tokens[0]);
			appendComponentNode(document, pkgElement, relId, tokens[1], "Delivery", null);
		}
		return pkgElement;
	}

	private String getWIPTId(String iptStr) {
		final String ipts = "[AKM][BCM][ECM][IAM][RFM][CS]";
		String locIPT = "[" + iptStr + "]";
		if(ipts.indexOf(locIPT) > -1) {
			return "REL_" + iptStr;
		}
		return null;
	}
	private Element appendAllWIPTS(Document document, Element rootElement) {
		String[] releases = new String[] { "AKM;AKM", "BCM;BCM", "ECM;ECM", "IAM;IAM", "RFM;RFM", "CS;CS" };
		
		Element pkgElement = appendRootPkgElement(document, rootElement, "WIPTPkg01", WIPT_FOLDER_NAME);
		for(String rel : releases) {
			String[] tokens = rel.split("[;]");
			String iptId = getWIPTId(tokens[0]);
			appendComponentNode(document, pkgElement, iptId, tokens[1], "Management Function", null);
		}
		return pkgElement;
	}
	
	private Element appendAllSecurityComponents(Document document, Element rootElement) {
		Element pkgElement = appendRootPkgElement(document, rootElement, "SecurityCompPkg01", SECCP_FOLDER_NAME);
		for(SecurityComp sec : soaData.secCompList) {
			String nameID = sec.getUMLId();	//SecurityComp.getUMLId(sec.id);
			Element appElement = appendClassNode(document, pkgElement, nameID, sec.name, SecurityComp.STEREO_TYPE, sec.desc);
			appendPropertyNode(document, appElement, sec, "Input", sec.procInput);
			appendPropertyNode(document, appElement, sec, "Output", sec.procOutput);
			appendPropertyNode(document, appElement, sec, "Supports Layer", sec.layer);
		}
		return pkgElement;
	}

	private Element appendAllIntegrationComponents(Document document, Element rootElement) {
		Element host_pkgElement = appendRootPkgElement(document, rootElement, "HostIntegrationCompPkg01", HOST_INTG_FOLDER_NAME);
		Element sec_pkgElement = appendRootPkgElement(document, rootElement, "SecIntegrationCompPkg01", SEC_INTG_FOLDER_NAME);
		Element int_pkgElement = appendRootPkgElement(document, rootElement, "IntegrationCompPkg01", INT_INTG_FOLDER_NAME);
		Element sw_pkgElement = appendRootPkgElement(document, rootElement, "SWIntegrationCompPkg01", SW_INTG_FOLDER_NAME);
		
		Element ucpkgElement = null;
		String stereoType = null;
		for(IntegrationComp icmp : soaData.intCompList) {
			String nameID = icmp.getUMLId();	//IntegrationComp.getUMLId(icmp.id);
			stereoType = IntegrationComp.STEREO_TYPE;
			if(!Utils.isEmpty(icmp.stereoType))
				stereoType = icmp.stereoType;
			if(stereoType.indexOf("Hosting Config") > -1)
				ucpkgElement = host_pkgElement;
			else if(stereoType.indexOf("Security Config") > -1)
				ucpkgElement = sec_pkgElement;
			else
				ucpkgElement = int_pkgElement;
			Element ucElement = appendClassNode(document, ucpkgElement, nameID, icmp.name, stereoType, icmp.desc);
			List<EntitiesReferences> entreflist = Utils.findEntitiesReferencesForRefType(soaData.entityRef, icmp.id, EntityType.INTSW, IntegrationSW.INT_TYPE);
			String dependencyQual = "";
			if(entreflist != null && entreflist.size() > 0) {
				dependencyQual = "Implemented by";
				for(EntitiesReferences sw : entreflist) {
					AbstractSOAData intUCObj = Utils.findReferedEntityObj(soaData, EntityType.INTSW, sw.refid);
					String intUCId = intUCObj.getUMLId(); //IntegrationSW.getUMLId(sw.refid);
					//String dependcyID = "Depend_" + EntityType.INTUC + "_" + EntityType.INTSW + "_" + icmp.id;
					String dependcyID = generateDependencyId(EntityType.INTUC, icmp, EntityType.INTSW, intUCObj, dependencyQual);			
					appendDependencyNode(document, ucpkgElement, dependcyID, dependencyQual, intUCId, nameID);
				}
			}
			entreflist = Utils.findEntitiesReferencesForRefType(soaData.entityRef, icmp.id, EntityType.INTSW, IntegrationSW.BUILD_TYPE);
			if(entreflist != null && entreflist.size() > 0) {
				dependencyQual = "Configured/Developed with";
				for(EntitiesReferences sw : entreflist) {
					AbstractSOAData intUCObj = Utils.findReferedEntityObj(soaData, EntityType.INTSW, sw.refid);
					String intUCId = intUCObj.getUMLId(); //IntegrationSW.getUMLId(sw.refid);
					//String dependcyID = "Depend_" + EntityType.INTUC + "_" + EntityType.INTSW + "_" + icmp.id;
					String dependcyID = generateDependencyId(EntityType.INTUC, icmp, EntityType.INTSW, intUCObj, dependencyQual);			
					appendDependencyNode(document, ucpkgElement, dependcyID, dependencyQual, intUCId, nameID);
				}
			}
			entreflist = Utils.findEntitiesReferencesForRefType(soaData.entityRef, icmp.id, EntityType.SECCP, SecurityComp.INT_TYPE);
			if(entreflist != null && entreflist.size() > 0) {
				dependencyQual = "Supports Security";
				for(EntitiesReferences sec : entreflist) {
					AbstractSOAData intUCObj = Utils.findReferedEntityObj(soaData, EntityType.SECCP, sec.refid);
					String intUCId = intUCObj.getUMLId(); //SecurityComp.getUMLId(sec.refid);
					//String dependcyID = "Depend_" + EntityType.INTUC + "_" + EntityType.SECCP + "_" + icmp.id;
					String dependcyID = generateDependencyId(EntityType.INTUC, icmp, EntityType.SECCP, intUCObj, dependencyQual);			
					appendDependencyNode(document, ucpkgElement, dependcyID, dependencyQual, intUCId, nameID);
				}
			}
			entreflist = Utils.findEntitiesReferencesForRefType(soaData.entityRef, icmp.id, EntityType.SECCP, SecurityComp.USED_TYPE);
			if(entreflist != null && entreflist.size() > 0) {
				dependencyQual = "Uses Security";
				for(EntitiesReferences sec : entreflist) {
					AbstractSOAData intUCObj = Utils.findReferedEntityObj(soaData, EntityType.SECCP, sec.refid);
					String intUCId = intUCObj.getUMLId(); //SecurityComp.getUMLId(sec.refid);
					//String dependcyID = "Depend_" + EntityType.INTUC + "_" + EntityType.SECCP + "_" + icmp.id;
					String dependcyID = generateDependencyId(EntityType.INTUC, icmp, EntityType.SECCP, intUCObj, dependencyQual);			
					appendDependencyNode(document, ucpkgElement, dependcyID, dependencyQual, intUCId, nameID);
				}
			}
		}
		for(IntegrationSW sw : soaData.intSWList) {
			String nameID = sw.getUMLId(); //IntegrationSW.getUMLId(sw.id);
			Element appElement = appendClassNode(document, sw_pkgElement, nameID, sw.name, IntegrationSW.STEREO_TYPE, sw.desc);
			appendPropertyNode(document, appElement, sw, "Source", sw.disposition);
			appendPropertyNode(document, appElement, sw, "Vendor", sw.swVendor);
			appendPropertyNode(document, appElement, sw, "OS", sw.os);
			appendPropertyNode(document, appElement, sw, "Version", sw.version);
			//added on 07/19/2010 - RSingh
			appendPropertyNode(document, appElement, sw, "Environment Type", sw.envType);
		}
		return sw_pkgElement;
	}
	
	private Element appendAllApplications(Document document, Element rootElement) {
		String[] applications = new String[] { "Account Management-Siebel", "Benefit Evidence Creation-WAS", 
				"Case Management-Siebel", "Enhanced EDMS-Documentum", "External  Notice-WAS",
				"Fee Processing-WAS", "Lottery-WAS", "Performance Analysis and Reporting-OBIE",
				"Policy Analyser-WAS,BE", "QA Review-WAS,BE", "Risk Analyser-WAS,BE,EAS" };

		Element pkgElement = appendRootPkgElement(document, rootElement, "ApplicationPkg01", APPS_FOLDER_NAME);
		for(Application app : soaData.appList) {
			String appID = app.getUMLId(); //Application.getUMLId(app.id);
			Element appElement = appendClassNode(document, pkgElement, appID, app.name, app.stereoType, app.desc);
			appendPropertyNode(document, appElement, app, "Database", app.database);
			appendPropertyNode(document, appElement, app, "Disposition", app.disposition);
			appendPropertyNode(document, appElement, app, "Relationship", app.relationship);
			
			String iptID = getWIPTId(app.wipt);
			if(Utils.isEmpty(iptID)) {
				appendPropertyNode(document, appElement, app, "Management Function", app.wipt);
			} else {
				//String realizeName = iptID + "_" + nameID;
				//appendRealizationNode(document, appElement, realizeName, iptID, nameID);
				/*
				 * replace dependency with "unidirectional" relationship - RSingh04/01/2010.
				String dependcyID = "Depend_WIPT_"+appID;
				appendDependencyNode(document, pkgElement, dependcyID, null, iptID, appID);
				*/
				appendUndirectedAssociationNode(document, pkgElement, appID, iptID, "");
			}
			String releaseID = getReleaseId(app.release);
			if(Utils.isEmpty(releaseID)) {
				loger.error("The Release Id [" + app.release + "] for Application [" + app.name + "] is invalid, so no association with release.");
			} else {
				/*
				 * replace dependency with "unidirectional" relationship - RSingh04/01/2010.
				String realizeName = releaseID + "_" + servId;
				appendRealizationNode(document, servElement, realizeName, releaseID, servId);
				*/
				appendUndirectedAssociationNode(document, pkgElement, appID, releaseID, "");
			}
			
			//find with no reference entity qualifier
			List<EntitiesReferences> entreflist = Utils.findEntitiesReferencesForRefType(soaData.entityRef, app.id, EntityType.INTUC, null);
			if(entreflist != null && entreflist.size() > 0) {
				String dependencyQual = "Hosts Application";
				AbstractSOAData intUCObj = Utils.findReferedEntityObj(soaData, EntityType.INTUC, entreflist.get(0).refid);
				String intUCId = intUCObj.getUMLId(); //IntegrationComp.getUMLId(entreflist.get(0).refid);
				//String dependcyID = "Depend_" + EntityType.APPL + "_" + EntityType.INTUC + "_" + app.id;
				String dependcyID = generateDependencyId(EntityType.APPL, app, EntityType.INTUC, intUCObj, dependencyQual);
				appendDependencyNode(document, pkgElement, dependcyID, dependencyQual, intUCId, appID);
			}
		}
		return pkgElement;
	}
	
	private Element appendAllInfrastructureComponents(Document document, Element rootElement) {
		Element rootPkgElement = appendRootPkgElement(document, rootElement, "InfrastructureCompPkg01", INF_FOLDER_NAME);
		//Element relPkgElement = appendRootPkgElement(document, rootPkgElement, "InfrastructureCompPkg00", "Relationships");
		Element relPkgElement = rootPkgElement;
		Element lsPkgElement = appendRootPkgElement(document, rootPkgElement, "InfrastructureCompPkg02", "Logical Servers");
		relPkgElement = lsPkgElement; //move associations to resp pkg for Shashi BIRT - 07/15/2010.
		
		String dependencyQual = "";

		for(InfrastructureLS lsComp : soaData.infLSList) {
			String nameID = lsComp.getUMLId(); //InfrastructureLS.getUMLId(lsComp.id);
			Element appElement = appendComponentNode(document, lsPkgElement, nameID, lsComp.name, InfrastructureLS.STEREO_TYPE, lsComp.desc);
			appendPropertyNode(document, appElement, lsComp, "Logical Server Type", lsComp.lsType);
			appendPropertyNode(document, appElement, lsComp, "HW", lsComp.hw);
			appendPropertyNode(document, appElement, lsComp, "Product", lsComp.product);
			appendPropertyNode(document, appElement, lsComp, "OS", lsComp.os);
			appendPropertyNode(document, appElement, lsComp, "Version", lsComp.version);

			List<EntitiesReferences> entreflist = Utils.findEntitiesReferencesForRefType(soaData.entityRef, lsComp.id, EntityType.INTUC, null);
			if(entreflist != null && entreflist.size() > 0) {
				dependencyQual = "Contains";
				for(EntitiesReferences sw : entreflist) {
					AbstractSOAData intLSObj = Utils.findReferedEntityObj(soaData, EntityType.INTUC, sw.refid);
					String intLSId = intLSObj.getUMLId(); //IntegrationComp.getUMLId(sw.refid);
					//String dependcyID = "Depend_" + EntityType.INFLS + "_" + EntityType.INTUC + "_" + lsComp.id;
					String dependcyID = generateDependencyId(EntityType.INFLS, lsComp, EntityType.INTUC, intLSObj, dependencyQual);			
					appendDependencyNode(document, relPkgElement, dependcyID, dependencyQual, intLSId, nameID);
				}
			}
			entreflist = Utils.findEntitiesReferencesForRefType(soaData.entityRef, lsComp.id, EntityType.SECCP, SecurityComp.SUP_TYPE);
			if(entreflist != null && entreflist.size() > 0) {
				dependencyQual = "Supports Security";
				for(EntitiesReferences sec : entreflist) {
					AbstractSOAData intSECObj = Utils.findReferedEntityObj(soaData, EntityType.SECCP, sec.refid);
					String intSECId = intSECObj.getUMLId(); //SecurityComp.getUMLId(sec.refid);
					//String dependcyID = "Depend_" + EntityType.INFLS + "_" + EntityType.SECCP + "_" + lsComp.id;
					String dependcyID = generateDependencyId(EntityType.INFLS, lsComp, EntityType.SECCP, intSECObj, dependencyQual);			
					appendDependencyNode(document, relPkgElement, dependcyID, dependencyQual, intSECId, nameID);
				}
			}
			entreflist = Utils.findEntitiesReferencesForRefType(soaData.entityRef, lsComp.id, EntityType.SECCP, SecurityComp.INT_TYPE);
			if(entreflist != null && entreflist.size() > 0) {
				dependencyQual = "Uses Security";
				for(EntitiesReferences sec : entreflist) {
					AbstractSOAData intSECObj = Utils.findReferedEntityObj(soaData, EntityType.SECCP, sec.refid);
					String intSECId = intSECObj.getUMLId(); //SecurityComp.getUMLId(sec.refid);
					//String dependcyID = "Depend_" + EntityType.INFLS + "_" + EntityType.SECCP + "_" + lsComp.id;
					String dependcyID = generateDependencyId(EntityType.INFLS, lsComp, EntityType.SECCP, intSECObj, dependencyQual);			
					appendDependencyNode(document, relPkgElement, dependcyID, dependencyQual, intSECId, nameID);
				}
			}
		}
		Element psPkgElement = appendRootPkgElement(document, rootPkgElement, "InfrastructureCompPkg03", "Physical Servers");
		relPkgElement = psPkgElement; //move associations to resp pkg for Shashi BIRT - 07/15/2010.
		String stereoType = null;
		for(InfrastructurePS psComp : soaData.infPSList) {
			String nameID = psComp.getUMLId(); //InfrastructurePS.getUMLId(psComp.id);
			stereoType = InfrastructurePS.STEREO_TYPE;
			if(!Utils.isEmpty(psComp.stereoType))
				stereoType = psComp.stereoType;
			Element psElement = appendClassNode(document, psPkgElement, nameID, psComp.name, stereoType, psComp.desc);

			appendPropertyNode(document, psElement, psComp, "Chipset", psComp.chipset);
			//appendPropertyNode(document, psElement, psComp, "Vendor", psComp.hwVendor);
			appendPropertyNode(document, psElement, psComp, "Hardware", psComp.hw);
			appendPropertyNode(document, psElement, psComp, "Product", psComp.product);
			appendPropertyNode(document, psElement, psComp, "OS", psComp.os);
			appendPropertyNode(document, psElement, psComp, "Version", psComp.version);

			List<EntitiesReferences> entreflist = Utils.findEntitiesReferencesForRefType(soaData.entityRef, psComp.id, EntityType.INFLS, null);
			if(entreflist != null && entreflist.size() > 0) {
				dependencyQual = "Hosts Server";
				for(EntitiesReferences sw : entreflist) {
					AbstractSOAData intLSObj = Utils.findReferedEntityObj(soaData, EntityType.INFLS, sw.refid);
					String intLSId = intLSObj.getUMLId(); //InfrastructureLS.getUMLId(sw.refid);
					//String dependcyID = "Depend_" + EntityType.INFPS + "_" + EntityType.INFLS + "_" + psComp.id;
					String dependcyID = generateDependencyId(EntityType.INFPS, psComp, EntityType.INFLS, intLSObj, dependencyQual);			
					appendDependencyNode(document, relPkgElement, dependcyID, dependencyQual, intLSId, nameID);
				}
			}
			entreflist = Utils.findEntitiesReferencesForRefType(soaData.entityRef, psComp.id, EntityType.SECCP, SecurityComp.SUP_TYPE);
			if(entreflist != null && entreflist.size() > 0) {
				dependencyQual = "Support Security";
				for(EntitiesReferences sec : entreflist) {
					AbstractSOAData intSECObj = Utils.findReferedEntityObj(soaData, EntityType.SECCP, sec.refid);
					String intSECId = intSECObj.getUMLId(); //SecurityComp.getUMLId(sec.refid);
					//String dependcyID = "Depend_" + EntityType.INFPS + "_" + EntityType.SECCP + "_" + psComp.id;
					String dependcyID = generateDependencyId(EntityType.INFPS, psComp, EntityType.SECCP, intSECObj, dependencyQual);			
					appendDependencyNode(document, relPkgElement, dependcyID, dependencyQual, intSECId, nameID);
				}
			}
			entreflist = Utils.findEntitiesReferencesForRefType(soaData.entityRef, psComp.id, EntityType.SECCP, SecurityComp.USED_TYPE);
			if(entreflist != null && entreflist.size() > 0) {
				dependencyQual = "Uses Security";
				for(EntitiesReferences sec : entreflist) {
					AbstractSOAData intSECObj = Utils.findReferedEntityObj(soaData, EntityType.SECCP, sec.refid);
					String intSECId = intSECObj.getUMLId(); //SecurityComp.getUMLId(sec.refid);
					//String dependcyID = "Depend_" + EntityType.INFPS + "_" + EntityType.SECCP + "_" + psComp.id;
					String dependcyID = generateDependencyId(EntityType.INFPS, psComp, EntityType.SECCP, intSECObj, dependencyQual);			
					appendDependencyNode(document, relPkgElement, dependcyID, dependencyQual, intSECId, nameID);
				}
			}
		}
		Element lsInstPkgElement = appendRootPkgElement(document, rootPkgElement, "InfrastructureCompPkg04", "Logical Server Instances");
		relPkgElement = lsInstPkgElement; //move associations to resp pkg for Shashi BIRT - 07/15/2010.
		for(InfrastructureLSInst psComp : soaData.infLSInstList) {
			String nameID = psComp.getUMLId();
			stereoType = InfrastructureLSInst.STEREO_TYPE;
			if(!Utils.isEmpty(psComp.stereoType))
				stereoType = psComp.stereoType;
			Element psElement = appendClassNode(document, lsInstPkgElement, nameID, psComp.name, stereoType, null);

			appendPropertyNode(document, psElement, psComp, "CPU", psComp.CPU);
			appendPropertyNode(document, psElement, psComp, "RAM", psComp.RAM);

			List<EntitiesReferences> relprocEnts = Utils.findEntitiesReferencesForSourceType(soaData.entityRef, psComp.id, EntityType.LSINST);
			if(relprocEnts != null) {
				for(EntitiesReferences pr : relprocEnts) {
					AbstractSOAData relProcEntObj = Utils.findReferedEntityObj(soaData, Constants.convert(pr.reftype), pr.refid);
					String relProcEntId = relProcEntObj.getUMLId(); //pr.reftype + "_" + pr.refid;
					String verb = "";
					if(Constants.getInfPSType().equalsIgnoreCase(pr.reftype))
						verb = "Hosted on";
					else if(Constants.getInfLSType().equalsIgnoreCase(pr.reftype))
						verb = "Image of";
					
					String dependcyID = generateDependencyId(EntityType.LSINST, psComp, Constants.convert(pr.reftype), relProcEntObj, verb);			
					appendDependencyNode(document, relPkgElement, dependcyID, verb, relProcEntId, nameID);
				}
			}
		}
		return rootPkgElement;
	}

	private Element appendAllActors(Document document, Element rootElement) {
		Element pkgElement = appendRootPkgElement(document, rootElement, "ActorsPkg01", ACTORS_FOLDER_NAME);
		HashMap<String, Element> addedActMap = new HashMap<String, Element>();
		for(Actor a : soaData.actorList) {
			if(a.childrenList != null && a.childrenList.size() > 0)
				continue;
			String actorId = a.getUMLId(); //Actor.getUMLId(a.id);
			Element addedElement = appendActorNode(document, pkgElement, actorId, a.name, a.roleType, a.desc);
			addedActMap.put(actorId, addedElement);
		}
		for(Actor a : soaData.actorList) {
			if(a.childrenList == null || a.childrenList.size() == 0)
				continue;
			String actorId = a.getUMLId(); //Actor.getUMLId(a.id);
			if(addedActMap.get(actorId) == null) {
				Element addedElement = appendAbstractActorNode(document, pkgElement, actorId, a.name, "Generic", a.desc);
				addedActMap.put(actorId, addedElement);
			}
			for(Actor parent : a.childrenList) {
				String childActorId = parent.getUMLId(); //Actor.getUMLId(parent.id);
				Element servElement = addedActMap.get(childActorId);
				if(servElement == null) {
					servElement = appendActorNode(document, pkgElement, childActorId, parent.name, a.roleType, parent.desc);
					addedActMap.put(childActorId, servElement);
				}
				appendGeneralizationNode(document, servElement, childActorId, actorId);
			}
		}
		return pkgElement;
	}
	
	private Element appendAllUseCase(Document document, Element rootElement) {
		Element rpkgElement = appendRootPkgElement(document, rootElement, "UseCasePkg01", USECASE_FOLDER_NAME);
		Element bd_pkgElement = appendRootPkgElement(document, rpkgElement, "UseCasePkg03", "To Review");
		Element prop_pkgElement = appendRootPkgElement(document, rpkgElement, "UseCasePkg02", "Assessed");

		Element pkgElement = null;
		for(UseCase u : soaData.useCaseList) {
			pkgElement = bd_pkgElement;
			if(u.status.indexOf("Assessed") > -1)
				pkgElement = prop_pkgElement;
			String usecaseID = u.getUMLId(); //UseCase.getUMLId(u.uname);	//UseCase.getUMLId(u.id);
			//Element usecaseElement = appendUseCaseNode(document, pkgElement, usecaseID, u.uname, u.unumber, u.desc);
			Element usecaseElement = appendUseCaseNode(document, pkgElement, usecaseID, u.uname, u.stereotype, u.desc);
//			appendPropertyNode(document, usecaseElement, EntityType.PROC, u.id, "ID", u.unumber);
//			appendPropertyNode(document, usecaseElement, EntityType.PROC, u.id, "Status", u.status);
//			appendPropertyNode(document, usecaseElement, EntityType.PROC, u.id, "Release", u.release);
			String releaseID = getReleaseId(u.release);
			if(Utils.isEmpty(releaseID)) {
				loger.error("The Release Id [" + u.release + "] for UseCase [" + u.uname + "] is invalid, so no association with release.");
			} else {
				/*
				 * replace dependency with "unidirectional" relationship - RSingh04/01/2010.
				String realizeName = releaseID + "_" + usecaseID;
				appendRealizationNode(document, usecaseElement, realizeName, releaseID, usecaseID);
				*/
				appendUndirectedAssociationNode(document, pkgElement, usecaseID, releaseID, "");
			}
			//String iptID = getWIPTId(u.unumber.substring(0, 3));	this is old way of deriving ipt from number - rsingh 3/31/10
			String iptID = getWIPTId(u.wipt);
			if(!Utils.isEmpty(iptID)) {
				/*
				 * replace dependency with "unidirectional" relationship - RSingh04/01/2010.
				String realizeName = iptID + "_" + usecaseID;
				appendRealizationNode(document, usecaseElement, realizeName, iptID, usecaseID);
				*/
				appendUndirectedAssociationNode(document, pkgElement, usecaseID, iptID, "");
			}
			List<EntitiesReferences> relEntsList = Utils.findEntitiesReferencesForSourceType(soaData.entityRef, u.id, EntityType.USEC);
			if(relEntsList != null) {
				String dependcyID = null;
				String relUCEntId = null;
				AbstractSOAData relUCEntObj = null;
				String verb = null;
				for(EntitiesReferences refEnt : relEntsList) {
					relUCEntObj = Utils.findReferedEntityObj(soaData, Constants.convert(refEnt.reftype), refEnt.refid);
					relUCEntId = relUCEntObj.getUMLId(); //refEnt.reftype + "_" + refEnt.refid;
					if(Constants.getActorType().equalsIgnoreCase(refEnt.reftype)) {
						verb = "acts";
					}
					//dependcyID = "Depend_UseCase_"+u.id;
					dependcyID = generateDependencyId(EntityType.USEC, u, Constants.convert(refEnt.reftype), relUCEntObj, verb);			
					appendDependencyNode(document, pkgElement, dependcyID, verb, relUCEntId, usecaseID);
				}
			}
		}
		return rpkgElement;
	}
	
	private Element appendAllBusinessProcs(Document document, Element rootElement) {
		Element rpkgElement = appendRootPkgElement(document, rootElement, "BusProcPkg01", BUSPROC_FOLDER_NAME);

		Element pkgElement = null, subprocGrpElement = null, activityGrpElement = null;
		String bProcId = null;
		String dispName = null;
		for(BusinessProcess bp : soaData.bProcessList) {
			bProcId = bp.getUMLId(); //BusinessProcess.getUMLId(bp.id);
			dispName = bp.serialNum + " " + bp.name;
			if (bp.function.equalsIgnoreCase("MANAGEMENT FUNCTION")) {
				pkgElement = appendRootPkgElement(document, rpkgElement, bProcId, dispName);
			} else if (bp.function.equalsIgnoreCase("Sub-Process")) {
				subprocGrpElement = appendActivityNode(document, pkgElement, bProcId, dispName, "Sub-Process", null);
			} else if (bp.function.equalsIgnoreCase("Activity")) {
				activityGrpElement = appendNodeElement(document, subprocGrpElement, "uml:StructuredActivityNode", bProcId, dispName, "Activity", null);
			} else {
				appendNodeElement(document, activityGrpElement, "uml:OpaqueAction", bProcId, dispName, "Task", null);
			}
		}
		return rpkgElement;
	}

	private Element appendAllServices(Document document, Element rootElement) {
		Element pkgElement = appendRootPkgElement(document, rootElement, "ServesPkg01", SERVES_FOLDER_NAME);
		
		for(Service s : soaData.servList) {
			String servId = s.getUMLId(); //Service.getUMLId(s.id);
			
			Element servElement = appendClassNode(document, pkgElement, servId, s.name, s.stereotype, s.desc);
			
			appendPropertyNode(document, servElement, s, "Maturity", ""+s.maturity);
			appendPropertyNode(document, servElement, s, "Explosure", s.exposure);
			appendPropertyNode(document, servElement, s, "Realization", s.realization);
			//changed to attrib from association - 1/25/2010
			appendPropertyNode(document, servElement, s, "Exposes Capability", s.extsystems);
			
			List<RelatedProperties> propsList = Utils.findAllProps(soaData.propsList, EntityType.SERV, s.id);
			for (RelatedProperties r : propsList) {
				appendPropertyNode(document, servElement, s, r.propName, r.propValue);
			}

			String iptID = getWIPTId(s.wipt);
			if(Utils.isEmpty(iptID)) {
				appendPropertyNode(document, servElement, s, "IPT", s.wipt);
			} else {
				//String realizeName = iptID + "_" + servId;
				//appendRealizationNode(document, servElement, realizeName, iptID, servId);
				/*
				 * replace dependency with "unidirectional" relationship - RSingh04/01/2010.
				String dependcyID = "Depend_WIPT_"+servId;
				appendDependencyNode(document, pkgElement, dependcyID, null, iptID, servId);
				*/
				appendUndirectedAssociationNode(document, pkgElement, servId, iptID, "");
			}
			String releaseID = getReleaseId(s.release);
			if(Utils.isEmpty(releaseID)) {
				loger.error("The Release Id [" + s.release + "] for Service [" + s.name + "] is invalid, so no association with release.");
			} else {
				/*
				 * replace dependency with "unidirectional" relationship - RSingh04/01/2010.
				String realizeName = releaseID + "_" + servId;
				appendRealizationNode(document, servElement, realizeName, releaseID, servId);
				*/
				appendUndirectedAssociationNode(document, pkgElement, servId, releaseID, "");
			}
			/*
			String dataList = s.operations;
			if(!Utils.isEmpty(dataList)) {
				String[] opList = dataList.split("[,]");
				for(String opName : opList)
					appendOperationNode(document, servElement, EntityType.SERV, opName.trim());
			}
			*/
			if(s.opList != null && s.opList.size() > 0) {
				for(ServiceOperation sOp : s.opList) {
					Element servOpElem = appendOperationNode(document, servElement, EntityType.SERV, sOp.name.trim(), s.getHashableId()/*ServiceOperation.getUMLId(sOp.id)*/);
					List<RelatedDataReference> dataRef = Utils.findAllRelatedDataEntities(soaData.dataRefList, EntityType.SOPER, sOp.id);
					for (RelatedDataReference r : dataRef) {
						String relDataEntId = r.data.getUMLId(); //DataClass.getUMLId(r.data.id);
						String direction = "inout";
						if(RelatedDataReference.IN_TYPE.equalsIgnoreCase(r.direction)) {
							direction = "in";
						} else if(RelatedDataReference.OUT_TYPE.equalsIgnoreCase(r.direction)) {
							direction = "out";
						}
						appendOperationParamNode(document, servOpElem, EntityType.SERV, r.data.cname, relDataEntId, direction);
					}
				}
			}
			String dataList = s.applications;
			if(!Utils.isEmpty(dataList)) {
				String[] appList = dataList.split("[,]");
				String dependencyQual = "Exposes Capability";
				for(String appName : appList) {
					Application app = Utils.findApplicationByName(appName, soaData.appList);
					if(app == null) {
						loger.error("Unable to locate Application [" + appName + "] for Service [" + s.name + "]");
						continue;
					}
					String appNameID = app.getUMLId(); //Application.getUMLId(app.id);
					//String dependcyID = "Depend_Application_"+s.id;
					String dependcyID = generateDependencyId(EntityType.SERV, s, EntityType.APPL, app, dependencyQual);			
					appendDependencyNode(document, pkgElement, dependcyID, dependencyQual, appNameID, servId);
				}
			}
			/* Not an association, just an attrib as of 1/25/2010
			dataList = s.extsystems;
			if(!Utils.isEmpty(dataList)) {
				String[] appList = dataList.split("[,]");
				for(String appName : appList) {
					Application app = Utils.findApplicationByName(appName, soaData.appList);
					if(app == null) {
						loger.error("Unable to locate External System Application [" + appName + "] for Service [" + s.name + "]");
						continue;
					}
					String appNameID = Application.getUMLId(app.id);
					String dependcyID = "Depend_Application_"+s.id;
					appendDependencyNode(document, pkgElement, dependcyID, "Exposes Capability", appNameID, servId);
				}
			}
			*/
			/*
			 * Moved to Entity depend relation table.. RSingh 04/09/10
			 *
			dataList = s.otherservices;
			if(!Utils.isEmpty(dataList)) {
				String[] otherServList = dataList.split("[,]");
				for(String serv : otherServList) {
					Service oServ = Utils.findServiceByName(serv, soaData.servList);
					if(oServ == null) {
						loger.error("Unable to locate other service [" + serv + "] for Service [" + s.name + "]");
						continue;
					}
					String appNameID = Service.getUMLId(oServ.id);
					String dependcyID = "Depend_Service_"+s.id;
					appendDependencyNode(document, pkgElement, dependcyID, "Invokes Service", appNameID, servId);
				}
			}
			*/
			//moved here UML Creator to create Interface dependency with in Service definition 03/23/2010.			
			List<EntitiesReferences> relEntsList = Utils.findEntitiesReferencesForSourceType(soaData.entityRef, s.id, EntityType.SERV);
			if(relEntsList != null) {
				String dependcyID = null;
				String relINTREntId = null;
				AbstractSOAData relINTREntObj = null;
				String verb = null;
				for(EntitiesReferences refEnt : relEntsList) {
					verb = "";
					relINTREntObj = Utils.findReferedEntityObj(soaData, Constants.convert(refEnt.reftype), refEnt.refid);
					relINTREntId = relINTREntObj.getUMLId(); //refEnt.reftype + "_" + refEnt.refid;
					if(Constants.getInterfaceType().equalsIgnoreCase(refEnt.reftype)) {
						verb = "Invokes Interface";
					} else if(Constants.getServiceType().equalsIgnoreCase(refEnt.reftype)) {
						verb = "Invokes Service";
					} else if(Constants.getUseCaseType().equalsIgnoreCase(refEnt.reftype)) {
						verb = "Implements Service Definition";
					} else if(Constants.getAgentType().equalsIgnoreCase(refEnt.reftype)) {
						verb = "Implemented by Agent";
					}
					//dependcyID = "Depend_Service_"+s.id;
					dependcyID = generateDependencyId(EntityType.SERV, s, Constants.convert(refEnt.reftype), relINTREntObj, verb);			
					appendDependencyNode(document, pkgElement, dependcyID, verb, relINTREntId, servId);
				}
			}
			dataList = s.inthostcfg;
			if(!Utils.isEmpty(dataList)) {
				String[] iCompList = dataList.split("[,]");
				String dependencyQual = "Hosts Service";
				for(String comp : iCompList) {
					IntegrationComp iComp = Utils.findIntegrationUCByName("Hosting Config", comp, soaData.intCompList);
					if(iComp == null) {
						loger.error("Unable to locate Integration Component of type 'Hosts Service' [" + comp + "] for Service [" + s.name + "]");
						continue;
					}
					String appNameID = iComp.getUMLId(); //IntegrationComp.getUMLId(iComp.id);
					//String dependcyID = "Depend_ICompH_"+s.id;
					String dependcyID = generateDependencyId(EntityType.SERV, s, EntityType.INTUC, iComp, dependencyQual);			
					appendDependencyNode(document, pkgElement, dependcyID, dependencyQual, appNameID, servId);
				}
			}
			dataList = s.intesbendpt;
			if(!Utils.isEmpty(dataList)) {
				String[] iCompList = dataList.split("[,]");
				String dependencyQual = "Accessed Via";
				for(String comp : iCompList) {
					IntegrationComp iComp = Utils.findIntegrationUCByName("Integration Config", comp, soaData.intCompList);
					if(iComp == null) {
						loger.error("Unable to locate Integration Component of type 'Integration Config' [" + comp + "] for Service [" + s.name + "]");
						continue;
					}
					String appNameID = iComp.getUMLId(); //IntegrationComp.getUMLId(iComp.id);
					//String dependcyID = "Depend_ICompE_"+s.id;
					String dependcyID = generateDependencyId(EntityType.SERV, s, EntityType.INTUC, iComp, dependencyQual);			
					appendDependencyNode(document, pkgElement, dependcyID, dependencyQual, appNameID, servId);
				}
			}
			List<RelatedDataReference> dataRef = Utils.findAllRelatedDataEntities(soaData.dataRefList, EntityType.SERV, s.id);
			for (RelatedDataReference r : dataRef) {
				String relDataEntId = r.data.getUMLId(); //DataClass.getUMLId(r.data.id);

				String verb = "uses";
				if(RelatedDataReference.IN_TYPE.equalsIgnoreCase(r.direction)) {
					verb = "Receive Data";
				} else if(RelatedDataReference.OUT_TYPE.equalsIgnoreCase(r.direction)) {
					verb = "Reply Data";
				}
				//String dependcyID = "Depend_" + EntityType.SERV + "_" + r.refid;
				String dependcyID = generateDependencyId(EntityType.SERV, s, EntityType.DATA, r.data, verb);			
				appendDependencyNode(document, pkgElement, dependcyID, verb, relDataEntId, servId);
			}
			/*
			 * Moved to Entity depend relation table.. RSingh 04/09/10
			 *			
			dataList = s.servDefID;
			if(!Utils.isEmpty(dataList)) {
				String[] otherUCList = dataList.split("[,]");
				for(String uc : otherUCList) {
					UseCase u = Utils.findUseCaseByNumber(uc, soaData.useCaseList);
					if(u == null) {
						loger.error("Unable to locate UseCase Service Defination [" + uc + "] for Service [" + s.name + "]");
						continue;
					}
					String uID = UseCase.getUMLId(u.id);
					String dependcyID = "Depend_UC_SD_"+s.id;
					appendDependencyNode(document, pkgElement, dependcyID, "Implements Service Definition", uID, servId);
				}
			}
			dataList = s.agent;
			if(!Utils.isEmpty(dataList)) {
				String[] otherAgentList = dataList.split("[,]");
				for(String agnt : otherAgentList) {
					Agent a = Utils.findAgentByName(null, agnt, soaData.agentList);
					if(a == null) {
						loger.error("Unable to locate Agent [" + agnt + "] for Service [" + s.name + "]");
						continue;
					}
					String agntID = Agent.getUMLId(a.id);
					String dependcyID = "Depend_AGNT_"+s.id;
					appendDependencyNode(document, pkgElement, dependcyID, "Implemented by Agent", agntID, servId);
				}
			}
			*/
		}
		return pkgElement;
	}
	
	private Element appendAllDataClasses(Document document, Element rootElement) {
		Element rootPkgElement = appendRootPkgElement(document, rootElement, "DataClassPkg01", DATA_FOLDER_NAME);
		Element persistpkgElement = appendRootPkgElement(document, rootPkgElement, "PersistedDataClassPkg01", DataClass.PERSISTED_TYPE);
		Element ioeTOpkgElement = appendRootPkgElement(document, rootPkgElement, "IOETODataClassPkg01", DataClass.IOE_TO_TYPE);
		Element nonioeTOpkgElement = appendRootPkgElement(document, rootPkgElement, "NONIOETODataClassPkg01", DataClass.NON_IOE_TO_TYPE);
		
		for(DataClass d : soaData.dataList) {
			String dataId = d.getUMLId(); //DataClass.getUMLId(d.id);
			Element dataElement = null;
			Element pkgElement = null;
			
			if(DataClass.PERSISTED_TYPE.equalsIgnoreCase(d.groupType)) 
				pkgElement = persistpkgElement;
			else if(DataClass.IOE_TO_TYPE.equalsIgnoreCase(d.groupType)) 
				pkgElement = ioeTOpkgElement;
			else
				pkgElement = nonioeTOpkgElement;
			
			dataElement = appendClassNode(document, pkgElement, dataId, d.cname, d.stereoType, d.desc);
			String iptID = getWIPTId(d.wipt);
			if(Utils.isEmpty(iptID)) {
				appendPropertyNode(document, dataElement, d, "IPT", d.wipt);
			} else {
				//String realizeName = iptID + "_" + dataId;
				//appendRealizationNode(document, dataElement, realizeName, iptID, dataId);
				/*
				 * replace dependency with "unidirectional" relationship - RSingh04/01/2010.
				String dependcyID = "Depend_WIPT_"+dataId;
				appendDependencyNode(document, pkgElement, dependcyID, null, iptID, dataId);
				*/
				appendUndirectedAssociationNode(document, pkgElement, dataId, iptID, "");
			}
			//appendPropertyNode(document, dataElement, EntityType.DATA, d.id, "Access Method", d.operations);
			
			String dataList = d.operations;
			if(!Utils.isEmpty(dataList)) {
				String[] opList = dataList.split("[,]");
				for(String opName : opList)
					appendOperationNode(document, dataElement, EntityType.DATA, opName, d.getHashableId());
			}

			dataList = d.application;
			if(!Utils.isEmpty(dataList)) {
				String[] appList = dataList.split("[,]");
				String dependencyQual = "Governs Data";
				for(String appName : appList) {
					Application app = Utils.findApplicationByName(appName, soaData.appList);
					if(app == null) {
						loger.error("Unable to locate Application [" + appName + "] for DataClass [" + d.cname + "]");
						continue;
					}
					String appNameID = app.getUMLId(); //Application.getUMLId(app.id);
					//String dependcyID = "Depend_Application_"+d.id;
					String dependcyID = generateDependencyId(EntityType.DATA, d, EntityType.APPL, app, dependencyQual);
					appendDependencyNode(document, pkgElement, dependcyID, dependencyQual, appNameID, dataId);
				}
			}
			dataList = d.integrationCfg;
			if(!Utils.isEmpty(dataList)) {
				String[] iCompList = dataList.split("[,]");
				String dependencyQual = "Hosts Data";
				for(String comp : iCompList) {
					IntegrationComp iComp = Utils.findIntegrationUCByName("Hosting Config", comp, soaData.intCompList);
					if(iComp == null) {
						loger.error("Unable to locate Integration Component of type 'Hosts Service' [" + comp + "] for DataClass [" + d.cname + "]");
						continue;
					}
					String appNameID = iComp.getUMLId(); //IntegrationComp.getUMLId(iComp.id);
					//String dependcyID = "Depend_ICompH_"+d.id;
					String dependcyID = generateDependencyId(EntityType.DATA, d, EntityType.INTUC, iComp, dependencyQual);
					appendDependencyNode(document, pkgElement, dependcyID, dependencyQual, appNameID, dataId);
				}
			}
		}
		return rootPkgElement;
	}
	
	private Element appendAllAgents(Document document, Element rootElement) {
		Element pkgElement = appendRootPkgElement(document, rootElement, "AgentPkg01", AGENTS_FOLDER_NAME);

		for(Agent a : soaData.agentList) {
			String agentId = a.getUMLId(); //Agent.getUMLId(a.id);
			Element agentElement = appendClassNode(document, pkgElement, agentId, a.name, a.stereoType, a.desc);
			appendPropertyNode(document, agentElement, a, "Existence", a.existence);
			appendPropertyNode(document, agentElement, a, "Release", a.release);
			String releaseID = getReleaseId(a.release);
			if(Utils.isEmpty(releaseID)) {
				loger.error("The Release Id [" + a.release + "] for Agent [" + a.name + "] is invalid, so no association with release.");
			} else {
				//String realizeName = releaseID + "_" + agentId;
				//appendRealizationNode(document, agentElement, realizeName, releaseID, agentId);
				/*
				 * replace dependency with "unidirectional" relationship - RSingh04/01/2010.
				String dependcyID = "Depend_Release_"+agentId;
				appendDependencyNode(document, pkgElement, dependcyID, null, releaseID, agentId);
				*/
				appendUndirectedAssociationNode(document, pkgElement, agentId, releaseID, "");
			}
			List<RelatedDataReference> dataRef = Utils.findAllRelatedDataEntities(soaData.dataRefList, EntityType.AGNT, a.id);
			for (RelatedDataReference r : dataRef) {
				String relDataEntId = r.data.getUMLId(); //DataClass.getUMLId(r.data.id);
				String verb = "uses";
				if(RelatedDataReference.IN_TYPE.equalsIgnoreCase(r.direction)) {
					verb = "Initiator Data";
				} else if(RelatedDataReference.OUT_TYPE.equalsIgnoreCase(r.direction)) {
					verb = "Responder Data";
				}
				//String dependcyID = "Depend_" + EntityType.AGNT + "_" + r.refid;
				String dependcyID = generateDependencyId(EntityType.AGNT, a, EntityType.DATA, r.data, verb);
				appendDependencyNode(document, pkgElement, dependcyID, verb, relDataEntId, agentId);
			}
			List<EntitiesReferences> relprocEnts = Utils.findEntitiesReferencesForSourceType(soaData.entityRef, a.id, EntityType.AGNT);
			if(relprocEnts != null) {
				for(EntitiesReferences pr : relprocEnts) {
					AbstractSOAData relProcEntObj = Utils.findReferedEntityObj(soaData, Constants.convert(pr.reftype), pr.refid);
					String relProcEntId = relProcEntObj.getUMLId(); //pr.reftype + "_" + pr.refid;
					String verb = "";
					if(Constants.getAgentType().equalsIgnoreCase(pr.reftype))
						verb = "Interacts with";
					else if(Constants.getIntegrationUCType().equalsIgnoreCase(pr.reftype))
						verb = "Hosted on";
					else if(Constants.getApplicationType().equalsIgnoreCase(pr.reftype))
						verb = "Interacts with";
					
					//String dependcyID = "Depend_Interface_"+a.id;
					String dependcyID = generateDependencyId(EntityType.INTR, a, Constants.convert(pr.reftype), relProcEntObj, verb);			
					appendDependencyNode(document, pkgElement, dependcyID, verb, relProcEntId, agentId);
				}
			}
		}
		return pkgElement;
	}
	
	private Element appendAllInterfaces(Document document, Element rootElement) {
		Element ui_pkgElement = appendRootPkgElement(document, rootElement, "UIInterfacePkg01", UI_INTERFACES_FOLDER_NAME);
		Element sys_pkgElement = appendRootPkgElement(document, rootElement, "SysInterfacePkg01", SYS_INTERFACES_FOLDER_NAME);
		Element ii_pkgElement = appendRootPkgElement(document, rootElement, "IiInterfacePkg01", II_INTERFACES_FOLDER_NAME);

		for(Interface i : soaData.intList) {
			Element pkgElement = null;
			if(i.stereotype.indexOf("Portal") > -1)
				pkgElement = ui_pkgElement;
			else if(i.stereotype.indexOf("System Interface") > -1)
				pkgElement = sys_pkgElement;
			else
				pkgElement = ii_pkgElement;
			String intId = i.getUMLId(); //Interface.getUMLId(i.id);
			Element intElement = appendClassNode(document, pkgElement, intId, i.name, i.stereotype, i.desc);
			appendPropertyNode(document, intElement, i, "Maturity", ""+i.maturity);
			appendPropertyNode(document, intElement, i, "Agency", i.agency);
			appendPropertyNode(document, intElement, i, "Data Flow", i.dirprimdataflow);
			appendPropertyNode(document, intElement, i, "MEP", i.mep);
			appendPropertyNode(document, intElement, i, "Type", i.neededby);

			String iptID = getWIPTId(i.wipt);
			if(Utils.isEmpty(iptID)) {
				appendPropertyNode(document, intElement, i, "IPT", i.wipt);
			} else {
				//String realizeName = iptID + "_" + procId;
				//appendRealizationNode(document, procElement, realizeName, iptID, procId);
				/*
				 * replace dependency with "unidirectional" relationship - RSingh04/01/2010.
				String dependcyID = "Depend_WIPT_"+procId;
				appendDependencyNode(document, pkgElement, dependcyID, null, iptID, procId);
				*/
				appendUndirectedAssociationNode(document, pkgElement, intId, iptID, "");
			}

			String releaseID = getReleaseId(i.release);
			if(Utils.isEmpty(releaseID)) {
				loger.error("The Release Id [" + i.release + "] for Interface [" + i.name + "] is invalid, so no association with release.");
			} else {
				//String realizeName = releaseID + "_" + intId;
				//appendRealizationNode(document, intElement, realizeName, releaseID, intId);
				/*
				 * replace dependency with "unidirectional" relationship - RSingh04/01/2010.
				String dependcyID = "Depend_Release_"+intId;
				appendDependencyNode(document, pkgElement, dependcyID, null, releaseID, intId);
				*/
				appendUndirectedAssociationNode(document, pkgElement, intId, releaseID, "");
			}
			List<RelatedProperties> propsList = Utils.findAllProps(soaData.propsList, EntityType.INTR, i.id);
			for (RelatedProperties r : propsList) {
				appendPropertyNode(document, intElement, i, r.propName, r.propValue);
			}

			List<RelatedDataReference> dataRef = Utils.findAllRelatedDataEntities(soaData.dataRefList, EntityType.INTR, i.id);
			for (RelatedDataReference r : dataRef) {
				String relDataEntId = r.data.getUMLId(); //DataClass.getUMLId(r.data.id);
				String verb = "uses";
				if(RelatedDataReference.IN_TYPE.equalsIgnoreCase(r.direction)) {
					verb = "Requests";
				} else if(RelatedDataReference.OUT_TYPE.equalsIgnoreCase(r.direction)) {
					verb = "Responds";
				}
				//String dependcyID = "Depend_" + EntityType.INTR + "_" + r.refid;
				String dependcyID = generateDependencyId(EntityType.INTR, i, EntityType.DATA, r.data, verb);			
				appendDependencyNode(document, pkgElement, dependcyID, verb, relDataEntId, intId);
			}
		/*
		 * MOVED to SOAData, to make Service as dependent on Interface - RSingh 03/23/2010 
			String dataList = i.serviceref;
			if(!Utils.isEmpty(dataList)) {
				String[] otherServList = dataList.split("[,]");
				for(String serv : otherServList) {
					Service oServ = Utils.findServiceByName(serv, soaData.servList);
					if(oServ == null) {
						loger.error("Unable to locate other service [" + serv + "] for Interface [" + i.name + "]");
						continue;
					}
					String appNameID = Service.getUMLId(oServ.id);
					String dependcyID = "Depend_Service_"+i.id;
					appendDependencyNode(document, pkgElement, dependcyID, "Invokes Interface", appNameID, intId);
				}
			}
			*/
			//added the missing association - 1/25/2010
			String dataList = i.sysextentity;
			if(!Utils.isEmpty(dataList)) {
				String[] appList = dataList.split("[,]");
				//Qualifier changed, identified wrong as part of BIRT reports by Kolli. RSingh - 5/18/10.
				String dependencyQual = "Integration with";	//"Exposes Capability";
				for(String appName : appList) {
					if("N/A".equalsIgnoreCase(appName) || "TBD".equalsIgnoreCase(appName)) {
						loger.warn("Ignore to locate External System Application [" + appName + "] for Interface [" + i.name + "]");
						continue;
					}
					Application app = Utils.findApplicationByName(appName, soaData.appList);
					if(app == null) {
						loger.error("Unable to locate External System Application [" + appName + "] for Interface [" + i.name + "]");
						continue;
					}
					String appNameID = app.getUMLId(); //Application.getUMLId(app.id);
					//String dependcyID = "Depend_Application_"+i.id;
					String dependcyID = generateDependencyId(EntityType.INTR, i, EntityType.APPL, app, dependencyQual);			
					appendDependencyNode(document, pkgElement, dependcyID, dependencyQual, appNameID, intId);
				}
			}
			List<EntitiesReferences> relprocEnts = Utils.findEntitiesReferencesForSourceType(soaData.entityRef, i.id, EntityType.INTR);
			if(relprocEnts != null) {
				for(EntitiesReferences pr : relprocEnts) {
					AbstractSOAData relProcEntObj = Utils.findReferedEntityObj(soaData, Constants.convert(pr.reftype), pr.refid);
					String relProcEntId = relProcEntObj.getUMLId(); //pr.reftype + "_" + pr.refid;
					String verb = "";
					if(Constants.getAgentType().equalsIgnoreCase(pr.reftype))
						verb = "Implemented by Agent";
					else if(Constants.getIntegrationUCType().equalsIgnoreCase(pr.reftype))
						verb = "Hosts Interface";
					else if(Constants.getActorType().equalsIgnoreCase(pr.reftype))
						verb = i.reltosys;					 
					else if(Constants.getUseCaseType().equalsIgnoreCase(pr.reftype)) //added on 07/19/2010 - RSingh
						verb = "Invokes Interface";
					else if(Constants.getProcessesType().equalsIgnoreCase(pr.reftype))
						verb = "Invokes Process";
					
					//String dependcyID = "Depend_Interface_"+i.id;
					String dependcyID = generateDependencyId(EntityType.INTR, i, Constants.convert(pr.reftype), relProcEntObj, verb);			
					appendDependencyNode(document, pkgElement, dependcyID, verb, relProcEntId, intId);
				}
			}
		}
		return ui_pkgElement;
	}
	
	private Element appendAllProcesses(Document document, Element rootElement) {
		Element pkgElement = appendRootPkgElement(document, rootElement, "ProcessPkg01", PROCESSES_FOLDER_NAME);
		for(Processes p : soaData.procList) {
			String procId = p.getUMLId(); //Processes.getUMLId(p.id);
			
			//Element procElement = appendActivityNode(document, pkgElement, procId, p.name, "Processes", p.desc);
			Element procElement = appendClassNode(document, pkgElement, procId, p.name, p.stereotype, p.desc);
			
			String iptID = getWIPTId(p.wipt);
			if(Utils.isEmpty(iptID)) {
				appendPropertyNode(document, procElement, p, "IPT", p.wipt);
			} else {
				//String realizeName = iptID + "_" + procId;
				//appendRealizationNode(document, procElement, realizeName, iptID, procId);
				/*
				 * replace dependency with "unidirectional" relationship - RSingh04/01/2010.
				String dependcyID = "Depend_WIPT_"+procId;
				appendDependencyNode(document, pkgElement, dependcyID, null, iptID, procId);
				*/
				appendUndirectedAssociationNode(document, pkgElement, procId, iptID, "");
			}
			appendPropertyNode(document, procElement, p, "Maturity", ""+p.maturity);
			appendPropertyNode(document, procElement, p, "Implementation", p.implMethod);

			String releaseID = getReleaseId(p.release);
			if(Utils.isEmpty(releaseID)) {
				loger.error("The Release Id [" + p.release + "] for Process [" + p.name + "] is invalid, so no association with release.");
			} else {
				//String realizeName = releaseID + "_" + procId;
				//appendRealizationNode(document, procElement, realizeName, releaseID, procId);
				/*
				 * replace dependency with "unidirectional" relationship - RSingh04/01/2010.
				String dependcyID = "Depend_Release_"+procId;
				appendDependencyNode(document, pkgElement, dependcyID, null, releaseID, procId);
				*/
				appendUndirectedAssociationNode(document, pkgElement, procId, releaseID, "");
			}
			String dataList = p.integrationCfg;
			if(!Utils.isEmpty(dataList)) {
				String[] iCompList = dataList.split("[,]");
				String dependencyQual = "Hosts Process";
				for(String comp : iCompList) {
					//This can be "Hosting Config" or "Integration Config" - RSingh 05/28/2010.
					//IntegrationComp iComp = Utils.findIntegrationUCByName("Hosting Config", comp, soaData.intCompList);
					IntegrationComp iComp = Utils.findIntegrationUCByName(null, comp, soaData.intCompList);
					if(iComp == null) {
						loger.error("Unable to locate Integration Component of type 'Hosts Service' [" + comp + "] for Process [" + p.name + "]");
						continue;
					}
					String appNameID = iComp.getUMLId(); //IntegrationComp.getUMLId(iComp.id);
					//String dependcyID = "Depend_ICompH_"+p.id;
					String dependcyID = generateDependencyId(EntityType.PROC, p, EntityType.INTUC, iComp, dependencyQual);			
					appendDependencyNode(document, pkgElement, dependcyID, dependencyQual, appNameID, procId);
				}
			}
			List<EntitiesReferences> relprocEnts = Utils.findEntitiesReferencesForSourceType(soaData.entityRef, p.id, EntityType.PROC);
			if(relprocEnts != null) {
				String dependcyID = null;
				String relProcEntId = null;
				AbstractSOAData relProcEntObj = null;
				String verb = null;
				String useCaseNum = null;
				for(EntitiesReferences pr : relprocEnts) {
					relProcEntObj = Utils.findReferedEntityObj(soaData, Constants.convert(pr.reftype), pr.refid);
					relProcEntId = relProcEntObj.getUMLId(); //pr.reftype + "_" + pr.refid;
					verb = "uses";
					if(Constants.getServiceType().equalsIgnoreCase(pr.reftype)) {
						verb = "Invokes Service";
					} else if(Constants.getInterfaceType().equalsIgnoreCase(pr.reftype)) {
						verb = "Invokes Process - del";
					} else if(Constants.getActorType().equalsIgnoreCase(pr.reftype)) {
						verb = "acts";
					} else if(Constants.getUseCaseType().equalsIgnoreCase(pr.reftype)) {
						useCaseNum = ((UseCase)relProcEntObj).unumber;
						//Commenting the mapping to Service Definitions (SD), leaving only UC mapping. As per request from Iam Morrison - 06/28/2010.
						if(useCaseNum.indexOf("-SD") > 0) {
							loger.warn("Process [" + p.name + "] has refeence to Service Definition [" + useCaseNum + "], ignoring the UML mapping.");
							continue;
						}
						verb = "implements";
					} else if(Constants.getBusinessProcessType().equalsIgnoreCase(pr.reftype)) {
						verb = "models";
					}
					//dependcyID = "Depend_Process_" + p.id;	// + "_" + relProcEntId;
					dependcyID = generateDependencyId(EntityType.PROC, p, Constants.convert(pr.reftype), relProcEntObj, verb);			
					appendDependencyNode(document, pkgElement, dependcyID, verb, relProcEntId, procId);
				}
			}
			
			List<RelatedProperties> propsList = Utils.findAllProps(soaData.propsList, EntityType.PROC, p.id);
			for (RelatedProperties r : propsList) {
				appendPropertyNode(document, procElement, p, r.propName, r.propValue);
			}
		}
		return pkgElement;
	}
	
	private Element appendAllEstimatingComponents(Document document, Element rootElement) {
		Element pkgElement = appendRootPkgElement(document, rootElement, "CompIDPkg01", COMPID_FOLDER_NAME);
		HashMap<String, Element> usedOnces = new HashMap<String, Element>();
		for(RelatedProperties p : soaData.propsList) {
			if(!p.propName.equalsIgnoreCase("COMP_ID"))
				continue;
			String propId = p.getUMLId(); //RelatedProperties.getUMLId(p.id);
			String propIdInMap = p.propValue;
			Element propElement = usedOnces.get(propIdInMap);
			if(propElement == null) {
				propElement = appendClassNode(document, pkgElement, propId, p.propValue, "Component Id", null);
				usedOnces.put(propIdInMap, propElement);
			}
			String verb = "part of";
			AbstractSOAData relProcEntObj = Utils.findReferedEntityObj(soaData, Constants.convert(p.refEntityType), p.refEntityId);
			String relProcEntId = relProcEntObj.getUMLId(); //p.refEntityType + "_" + p.refEntityId;
			//String dependcyID = "Depend_Prop_"+p.id;
			String dependcyID = generateDependencyId(EntityType.PROP, p, relProcEntObj.getUMLType(), relProcEntObj, verb);			
			String clientEntId = propElement.getAttribute("xmi:id");
			appendDependencyNode(document, pkgElement, dependcyID, verb, relProcEntId, clientEntId);
		}
		return pkgElement;
	}
}
